package com.ojoin.trade.supplyloan_core.web.service.impl;

import javax.jms.Destination;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.supplyloan_core.web.domain.CatchDataResultMessage;
import com.ojoin.trade.supplyloan_core.web.domain.Order;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.service.CatchDataMessageService;
import com.ojoin.trade.supplyloan_core.web.service.TbCatchdataStrategyService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlStattaskRunlogService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlStattaskRunresultService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyInventoryService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyPaymentService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyinfoCheckService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyloginCheckService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplysettleaccountCheckService;

@Service
public class CatchDataMessageServiceImpl implements CatchDataMessageService {

	@Autowired
	private TbSlSupplyPaymentService paymentService;

	@Autowired
	private TbSlSupplyInventoryService inventoryService;

	@Autowired
	public TbCatchdataStrategyService catchdataStrategyService;

	@Autowired
	private TbSlStattaskRunresultService runResultService;

	@Autowired
	private TbSlSupplyloginCheckService loginCheckService;
	@Autowired
	private TbSlSupplyinfoCheckService slinfoCheckService;
	@Autowired
	private TbSlSupplysettleaccountCheckService accountCheckService;

	@Autowired
	private TbSlStattaskRunlogService runlogService;
	
	@Autowired
	protected QueueSenderServieImpl queueSenderServie;

	@Autowired
	@Qualifier("catchDataTask")
	private Destination catchDataTask;
	
	

	private static Logger log = LoggerFactory
			.getLogger(CatchDataMessageServiceImpl.class);

	@Override
	public void filter(CatchDataResultMessage catchDataMessage) {

		LogUtils.info(log, catchDataMessage.toString());

		TbSlPlatformUser user = catchDataMessage.getPlatformUser();

		TbCatchdataStrategy ca = catchdataStrategyService
				.queryByPlatformUserId(user.getId());

		cleanLastSLData(user, ca);
		
		save(catchDataMessage);

		/**
		 * 业务统计
		 */

		if (runResultService.isCatchSucceed(catchDataMessage.getRunResult())) {
			
			Order order = new Order(Order.STATTOTAL);
			order.getParames().put("platformUserId", user.getId()+"");
			
			
			queueSenderServie.send(catchDataTask, order);
			
			//LogUtils.info(log, "chenckd and catchdata success , 业务统计");
			//catchdataStrategyService.stat(user, ca);
		}else {
			LogUtils.info(log, "抓取失败，不统计");
		}

		/**
		 * 更新抓取策略，是否为初次抓取
		 */

		afterCatchData(ca);

	}

	public void save(CatchDataResultMessage catchDataMessage) {
		// 插入现在数据
		runResultService.delete(catchDataMessage.getRunResult());
		runResultService.insert(catchDataMessage.getRunResult());

		paymentService.insertJDBCBatch(catchDataMessage.getSettlement());
		paymentService.insertJDBCBatch(catchDataMessage.getSettlementcheck());
		paymentService.insertJDBCBatch(catchDataMessage.getPayment());
		paymentService.insertJDBCBatch(catchDataMessage.getFinancing());
		
		paymentService.updateInBatch(catchDataMessage.getPayupdate());
		if (!CommonUtils.isEmpty(catchDataMessage.getInstore())) {
			inventoryService.insertJDBCBatch(catchDataMessage.getInstore());
		}

		inventoryService.insertJDBCBatch(catchDataMessage.getOutstore());
		inventoryService.updateJDBCBatch(catchDataMessage.getInventoryUpdate());
	}

	/**
	 * 删除重复或者历史数据
	 * 
	 * @param user
	 * @param ca
	 * @throws BusinessException
	 */
	public void cleanLastSLData(TbSlPlatformUser user, TbCatchdataStrategy ca)
			throws BusinessException {

		if (null == ca || ca.getIsInit().equals(TbCatchdataStrategy.noinit)) {

			inventoryService.delete(user.getId());
			paymentService.deleteByPlatformAccountId(user.getId());
		} else {
			if (ca.getInventoryCatchAllOrPart().equals(
					TbCatchdataStrategy.CatchAll)) {

				inventoryService.delete(user.getId(), TimeUtils.getCurDate());

			} else {
				inventoryService.deleteRepeat(user.getId());
			}

			if (ca.getPaymentCatchAllOrPart().equals(
					TbCatchdataStrategy.CatchAll)) {
				paymentService.deleteByPlatformAccountId(user.getId(),
						TimeUtils.getCurDate());
			} else {
				paymentService.deleteRepeat(user.getId());
			}
		}
	}

	public void afterCatchData(TbCatchdataStrategy ca) throws BusinessException {

		if (!CommonUtils.isEmpty(ca)
				&& ca.getIsInit().equals(TbCatchdataStrategy.noinit)) {

			ca.setIsInit(TbCatchdataStrategy.inited);
			catchdataStrategyService.updateById(ca);
		}
	}

}
